const express = require('express');
const operation = require('../controller/operation')
const handleUser = require('../controller/handleUser');
const { route } = require('./court');
const router = express.Router();
const md5 = require('md5-node');

// 获取所有用户的信息
router.get('/getInfo',(req,res)=>{
    
    operation.selectAll('user').then(data=>{
        res.json({
            status:200,
            data,
            msg:'获取用户数据成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'获取数据失败'
        })
    })

})
// 获取指定用户的信息
router.get('/getInfo/:uid',(req,res)=>{
    let {uid} = req.params
    operation.select('uid',uid,'user').then(result=>{
        res.json({
            status:200,
            data:result,
            msg:'获取用户信息成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'获取用户信息失败'
        })
    })
})
// 模糊查找用户信息
router.get('/search',(req,res)=>{
    let {query} = req.query;
    handleUser.searchUser(query).then(result=>{
        res.json({
            status:200,
            data:result,
            msg:'查询成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'查询失败'
        })
    })
})
// 删除用户信息
router.delete('/remove',(req,res)=>{
    let {query} = req.query;
    handleUser.removeUser(query).then(result=>{
        res.json({
            status:200,
            result,
            msg:'删除成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'删除失败'
        })
    })
})
//添加用户信息
router.post('/add',(req,res)=>{
    let data = {username,password} = req.body;
    operation.select('username',data.username,'user').then(exist=>{
        res.json({
            status:400,
            msg:'用户名已存在'
        })
    },noexist=>{
        handleUser.addUser(data).then(result=>{
            res.json({
                status:201,
                msg:'成功添加用户'
            })
        },err=>{
            res.json({
                status:400,
                msg:'添加失败'
            })
        })
       
    })
})
// 修改用户信息
router.put('/edit',(req,res)=>{
    let data = req.body;
    handleUser.editUser(data).then(result=>{
        res.json({
            status:200,
            msg:'修改用户信息成功'
        })
    },err=>{
        res.json({
            status:400,
            msg:'修改用户信息失败'
        })
    })
   
})
// 修改密码
router.put('/password',(req,res)=>{
    let data = req.body;
    if(data.role == '1'){
        operation.select('id',data.uid,'admin').then(result=>{
            console.log(result);
            if(result.password == md5(data.oldPassword)){   
                handleUser.editAdminPassword(md5(data.newPassword),data.uid).then(ok=>{
                    res.json({
                        status:200,
                        msg:ok
                    })
                },err=>{
                    res.json({
                        status:200,
                        msg:err
                    })
                })
              
            }else{
                res.json({
                    status:500,
                    msg:'旧密码输入错误'
                })
            }
           
        },err=>{
            res.json({
                status:500,
                msg:'修改失败'
            })
        })
    }
    else{   
        operation.select('uid',data.uid,'user').then(result=>{
        console.log(result);
        if(result.password == md5(data.oldPassword)){
            
            handleUser.editPassword(md5(data.newPassword),data.uid).then(ok=>{
                res.json({
                    status:200,
                    msg:ok
                })
            },err=>{
                res.json({
                    status:200,
                    msg:err
                })
            })
          
        }else{
            res.json({
                status:500,
                msg:'旧密码输入错误'
            })
        }
       
        
    },err=>{
        res.json({
            status:500,
            msg:'修改失败'
        })
    })
}
 

    
    
})
module.exports = router;